package com.rt.cloud.auth.config;

import com.rt.easyjava.common.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Component
public class MyUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Result result = restTemplate.getForObject("http://scm-epcodeless-core/core/innerService/getUserByLoginName/" + username, Result.class);

        Map<String, Object> userMap = (Map<String, Object>) result.getObj();
        if (userMap == null) {
            return null;
            // throw new UsernameNotFoundException("用户不存在！");
        }

        List<String> authList = (List<String>) userMap.get("menu2AuthList");
        String[] authArray = new String[authList.size()];
        authList.toArray(authArray);
        Collection<GrantedAuthority> grantedAuthorities = AuthorityUtils.createAuthorityList(authArray);

        String password = (String) userMap.get("passwordBcrypt");
        int deleteFlag = userMap.get("deleteFlag") == null ? 0 : (Integer) ((Integer) userMap.get("deleteFlag")).intValue();
        boolean enabled = deleteFlag == 0 ? true : false;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = true;

        User userDetails = new User(username, password, enabled, accountNonExpired,
                credentialsNonExpired, accountNonLocked, grantedAuthorities);

        return userDetails;
		/*
		A2UserDto userDto = menuService.getUserByLoginname(username);
		if(userDto == null){
			throw new UsernameNotFoundException("用户不存在！");
		}

		Set<String> authSet = userDto.getMenu2AuthList();
		String[] authArray =new String[authSet.size()];
		authSet.toArray(authArray);
		Collection<GrantedAuthority> grantedAuthorities = AuthorityUtils.createAuthorityList(authArray);

		String password = userDto.getPasswordBcrypt();
		boolean enabled = userDto.getDeleteFlag()==0? true: false;
		boolean accountNonExpired = true;
		boolean credentialsNonExpired = true;
		boolean accountNonLocked = true;

		User userDetails = new User(username, password, enabled, accountNonExpired,
				credentialsNonExpired, accountNonLocked,grantedAuthorities);

		return userDetails;
		*/


		/*return new User("admin123", passwordEncoder.encode("abc123456"),
				AuthorityUtils
						.commaSeparatedStringToAuthorityList("product,product:list,product:add"));
		*/
    }
}
